package com.example.custermbarview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
/**自定义柱状图*/
public class CustermBarView extends View {
	
	/** View的高度 **/
	private int mHeight;
	/** View的宽度 **/
	private int mWidth;
	/**柱子的根数*/
	private int barCount=7;
	/**空隙/柱子的百分比 1为相同,0.5为空隙是柱子宽度的50%*/
	private float barSpacePercent=0.5f;
	
	private Paint mPaint;
	/**Y轴最大值*/
	private double maxYvalue=55.0;

	private int[] testDataValue=new int[]{25,35,32,45,5,1,5};
	
	private boolean[] testDateBool=new boolean[]{true,false,true,false,true,true,true};
	
	private String[] testXlables=new String[]{"10/1","10/2","10/3","10/4","10/5","10/6","10/7"};
	
	/**X轴的Y坐标,用以显示x的坐标值和图*/
	private float XlabelHeight=20;
	
	private Bitmap xUpBitmap;
	/**Y值的格式化参数*/
	private String yFormat="%1s%%";
	
	private float addedHeight=30;
	
	public CustermBarView(Context context) {
		this(context,null);
	}
	public CustermBarView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
		mPaint.setStrokeWidth(2);
		
		BitmapDrawable  bitDrawable=(BitmapDrawable) getResources().getDrawable(R.drawable.icon_heart_normal);
		xUpBitmap=bitDrawable.getBitmap();
		
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int widthMode = MeasureSpec.getMode(widthMeasureSpec);
		int heightMode = MeasureSpec.getMode(heightMeasureSpec);
		int width = MeasureSpec.getSize(widthMeasureSpec);
		int height = MeasureSpec.getSize(heightMeasureSpec);
		if (widthMode == MeasureSpec.EXACTLY) {
			mWidth = width;
		} else {
			mWidth = 400;
		}
		if (heightMode == MeasureSpec.EXACTLY) {
			mHeight = height;
		} else {
			mHeight = 400;
		}
		setMeasuredDimension(mWidth, mHeight);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		Log.d("test", addedHeight+"");
		//画X轴
		canvas.drawLine(0, mHeight-XlabelHeight, mWidth, mHeight-XlabelHeight, mPaint);
		for (int i = 0; i < barCount; i++) {
			//画x坐标(文字,图片)
			float xStart=getXStart(i);
			mPaint.setTextSize(getBarWidth()/2);
			mPaint.setColor(Color.BLACK);
			canvas.drawText(testXlables[i], xStart, mHeight, mPaint);
			//画柱子
			mPaint.setColor(Color.parseColor("#007db3"));
			canvas.drawRect(xStart, getYTop(testDataValue[i])-addedHeight, xStart+getBarWidth(), mHeight-XlabelHeight, mPaint);
			//画X轴上的图标
			if(testDateBool[i]){
				canvas.drawBitmap(xUpBitmap, xStart+getBarWidth()/4,mHeight-XlabelHeight-xUpBitmap.getHeight(), mPaint);
			}
			//画柱子的值
			mPaint.setColor(Color.BLACK);
			canvas.drawText(String.format(yFormat, testDataValue[i]), xStart+getBarWidth()/4,getYTop(testDataValue[i])-addedHeight, mPaint);
		}
		
		super.onDraw(canvas);
	}
	
	/**获得每根柱子的起点*/
	private float getXStart(int index){
		if(index>=barCount){
			index=barCount-1;
		}
		return (float) (getBarWidth()*(1+barSpacePercent)*index+getBarWidth()*barSpacePercent/2.0);
	}
	/**柱子宽度*/
	private float getBarWidth(){
		return (float) (mWidth/((barCount*(1+barSpacePercent))*1.0));
	}
	
	/**获得柱子的高度*/
	private float getYTop(int Yvalue){
		if(Yvalue>maxYvalue){
			Yvalue=(int) maxYvalue;
		}
		return (float) (mHeight*(maxYvalue-Yvalue)/maxYvalue);
	}
	
	public void setYValueFormat(String format){
		
	}
	

}
